import {combineReducers} from 'redux-immutable'; // import Immutable from 'immutable';
import {
    GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN,
    GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS,
    GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL
} from 'Shared/actions/currentUserContext';
import Immutable from 'immutable';
import * as actions from './actions';

export const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(Immutable.fromJS({
                isFetching: true,
                message: ''
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: '',
                data: action.data
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: action.message
            }));
        default:
            return state;
    }
};

const initData = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
            });
        case actions.GET_INIT_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                message: '',
                currentCompany: action.data.currentCompany,
                companyType: action.data.companyType,
                userName: action.data.userName
            });
        case actions.GET_INIT_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });
        default:
            return state;
    }
};

const noticeData = (state, action) => {
    switch(action.type) {
        case actions.GET_NOTICE_DATA_BEGIN:
            return state.set('isFetching', true);

        case actions.GET_NOTICE_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });

        case actions.GET_NOTICE_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};

const claimSettlementData = (state, action) => {
    switch(action.type) {
        case actions.GET_CLAIMSETTLEMENT_DATA_BEGIN:
            return state.set('isFetching', true);

        case actions.GET_CLAIMSETTLEMENT_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });

        case actions.GET_CLAIMSETTLEMENT_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};

const workItemData = (state, action) => {
    switch(action.type) {
        case actions.GET_WORKITEM_DATA_BEGIN:
            return state.set('isFetching', true);

        case actions.GET_WORKITEM_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });

        case actions.GET_WORKITEM_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};

const expenseAdjustmentData = (state, action) => {
    switch(action.type) {
        case actions.GET_EXPENSE_DATA_BEGIN:
            return state.set('isFetching', true);

        case actions.GET_EXPENSE_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });

        case actions.GET_EXPENSE_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};

const userDefinedAssistPartData = (state, action) => {
    switch(action.type) {
        case actions.GET_ASSISTPART_DATA_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_ASSISTPART_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_ASSISTPART_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });
        default:
            return state;
    }
};
const partServicePriceData = (state, action) => {
    switch(action.type) {
        case actions.GET_SERVICEPRICE_DATA_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_SERVICEPRICE_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_SERVICEPRICE_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });
        default:
            return state;
    }
};

const partOrderPlanData = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTORDERPLAN_DATA_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTORDERPLAN_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTORDERPLAN_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });
        default:
            return state;
    }
};

const partBackOrderData = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTBACKORDER_DATA_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTBACKORDER_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTBACKORDER_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });
        default:
            return state;
    }
};

const partStockData = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTSTOCK_DATA_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTSTOCK_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTSTOCK_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });
        default:
            return state;
    }
};

const claimApplyData = (state, action) => {
    switch(action.type) {
        case actions.GET_CLAIMAPPLY_DATA_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_CLAIMAPPLY_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_CLAIMAPPLY_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const salePlanOrderSubmit = (state, action) => {
    switch(action.type) {
        case actions.GET_SALEPLANORDER_SUBMIT_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_SALEPLANORDER_SUBMIT_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_SALEPLANORDER_SUBMIT_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const salePlanOrderApprove = (state, action) => {
    switch(action.type) {
        case actions.GET_SALEPLANORDER_APPROVE_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_SALEPLANORDER_APPROVE_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_SALEPLANORDER_APPROVE_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partOrderPlanReject = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTORDERPLAN_REJECT_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTORDERPLAN_REJECT_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTORDERPLAN_REJECT_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partOrderPlanApprove = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTORDERPLAN_APPROVE_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTORDERPLAN_APPROVE_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTORDERPLAN_APPROVE_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partSaleOrderReject = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTSALEORDER_REJECT_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTSALEORDER_REJECT_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTSALEORDER_REJECT_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partSaleOrderApprove = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTSALEORDER_APPROVE_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTSALEORDER_APPROVE_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data.partSaleOrder
            });
        case actions.GET_PARTSALEORDER_APPROVE_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partOrderPIConfirm = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTORDERPI_CONFIRM_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTORDERPI_CONFIRM_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTORDERPI_CONFIRM_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partOrderPIPay = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTORDERPI_PAY_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTORDERPI_PAY_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTORDERPI_PAY_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partSaleOrderCreate = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTSALEORDER_CREATE_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTSALEORDER_CREATE_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data.partSaleOrder
            });
        case actions.GET_PARTSALEORDER_CREATE_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partOrderPICreate = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTORDERPI_CREATE_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTORDERPI_CREATE_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data.partOrderPI
            });
        case actions.GET_PARTORDERPI_CREATE_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const stockRequirementUpload = (state, action) => {
    switch(action.type) {
        case actions.GET_STOCKREQUIREMENT_UPLOAD_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_STOCKREQUIREMENT_UPLOAD_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_STOCKREQUIREMENT_UPLOAD_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const invoiceAddKd = (state, action) => {
    switch(action.type) {
        case actions.GET_INVOICEADDKD_SUCCESS:
            return state.merge({
                data: action.data
            });
        default:
            return state;
    }
};
const invoiceAddCbu = (state, action) => {
    switch(action.type) {
        case actions.GET_INVOICEADDCBU_SUCCESS:
            return state.merge({
                data: action.data
            });
        default:
            return state;
    }
};
const invoiceConfirmKd = (state, action) => {
    switch(action.type) {
        case actions.GET_INVOICECONFIRMKD_SUCCESS:
            return state.merge({
                data: action.data
            });
        default:
            return state;
    }
};
const invoiceConfirmCbu = (state, action) => {
    switch(action.type) {
        case actions.GET_INVOICECONFIRMCBU_SUCCESS:
            return state.merge({
                data: action.data
            });
        default:
            return state;
    }
};
const CBUSaleOrderReject = (state, action) => {
    switch(action.type) {
        case actions.GET_CBUSALEORDERREJECT_SUCCESS:
            return state.merge({
                data: action.data
            });
        default:
            return state;
    }
};
const KDSaleOrderReject = (state, action) => {
    switch(action.type) {
        case actions.GET_KDSALEORDERREJECT_SUCCESS:
            return state.merge({
                data: action.data
            });
        default:
            return state;
    }
};
const saleConfirmationRejectCbu = (state, action) => {
    switch(action.type) {
        case actions.GET_SALECONFIRMATIONREJECTCBU_SUCCESS:
            return state.merge({
                data: action.data
            });
        default:
            return state;
    }
};
const saleConfirmationRejectKd = (state, action) => {
    switch(action.type) {
        case actions.GET_SALECONFIRMATIONREJECTKD_SUCCESS:
            return state.merge({
                data: action.data
            });
        default:
            return state;
    }
};

const partClaimReject = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTCLAIM_REJECT_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTCLAIM_REJECT_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTCLAIM_REJECT_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partClaimDecisionConfirm = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTCLAIMDECISION_CONFIRM_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTCLAIMDECISION_CONFIRM_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTCLAIMDECISION_CONFIRM_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partClaimCacuConfirm = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTCLAIMCACU_CONFIRM_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTCLAIMCACU_CONFIRM_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTCLAIMCACU_CONFIRM_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partClaimApprove = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTCLAIM_APPROVE_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTCLAIM_APPROVE_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTCLAIM_APPROVE_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const packingListCabinetApply = (state, action) => {
    switch(action.type) {
        case actions.GET_PACKINGLISTCABINETAPPLY_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PACKINGLISTCABINETAPPLY_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PACKINGLISTCABINETAPPLY_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partShipOrderCreate = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTSHIPORDER_CREATE_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTSHIPORDER_CREATE_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTSHIPORDER_CREATE_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const exchangeRate = (state, action) => {
    switch(action.type) {
        case actions.GET_EXCHANGERATE_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_EXCHANGERATE_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_EXCHANGERATE_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const workingDay = (state, action) => {
    switch(action.type) {
        case actions.GET_WORKINGDAY_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_WORKINGDAY_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_WORKINGDAY_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const partShipmentReceipt = (state, action) => {
    switch(action.type) {
        case actions.GET_PARTSHIPMENT_RECEIPT_BEGIN:
            return state.set('isFetching', true);
        case actions.GET_PARTSHIPMENT_RECEIPT_SUCCESS:
            return state.merge({
                isFetching: false,
                data: action.data
            });
        case actions.GET_PARTSHIPMENT_RECEIPT_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });

        default:
            return state;
    }
};
const domainData = combineReducers({
    initData,
    permission,
    noticeData,
    workItemData,
    claimSettlementData,
    expenseAdjustmentData,
    userDefinedAssistPartData,
    partServicePriceData,
    partOrderPlanData,
    partBackOrderData,
    partStockData,
    claimApplyData,
    salePlanOrderSubmit,
    salePlanOrderApprove,
    partOrderPlanReject,
    partOrderPlanApprove,
    partSaleOrderReject,
    partSaleOrderApprove,
    partOrderPIConfirm,
    partOrderPIPay,
    partSaleOrderCreate,
    partOrderPICreate,
    stockRequirementUpload,
    invoiceAddKd,
    invoiceAddCbu,
    invoiceConfirmKd,
    invoiceConfirmCbu,
    CBUSaleOrderReject,
    KDSaleOrderReject,
    saleConfirmationRejectCbu,
    saleConfirmationRejectKd,
    partClaimReject,
    partClaimDecisionConfirm,
    partClaimCacuConfirm,
    partClaimApprove,
    packingListCabinetApply,
    partShipOrderCreate,
    exchangeRate,
    workingDay,
    partShipmentReceipt,
});
export default combineReducers({
    domainData
});
